package site.jlopen.controller.modules;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import site.jlopen.assets.config.ThreadAuthUser;
import site.jlopen.assets.oss.AwsOssConfig;
import site.jlopen.domain.data.event.KusciaDataEvent;
import site.jlopen.domain.datasource.event.KusciaDatasourceOssEvent;
import site.jlopen.entity.BaseResponse;
import site.jlopen.entity.data.DataInfoReq;
import site.jlopen.entity.data.TableStructure;
import site.jlopen.entity.file.FileUploadRes;
import site.jlopen.service.BaseService;
import site.jlopen.utils.csv.CsvUtil;

@RestController
@RequestMapping("/data")
public class DataController extends BaseService{
	private static Logger logger = LoggerFactory.getLogger(DataController.class);

	@PostMapping("/create")
	public BaseResponse<String> create(@RequestBody DataInfoReq req) {
		return dataService.createData(ThreadAuthUser.id(), req);
	}

	@GetMapping("/list")
	public List<KusciaDataEvent> list() {
		return dataService.getDatas(ThreadAuthUser.id());
	}

	@GetMapping("/notAuthData")
	public List<KusciaDataEvent> notAuthData(Long nodeId, Long projectId) {
		return dataService.getNotAuthDatas(nodeId, projectId);
	}

	@GetMapping("/remove")
	public BaseResponse<String> remove(Long dataId) {

		return dataService.remove(dataId);
	}

	@GetMapping("/restatus")
	public BaseResponse<String> restatus(Long dataId) {
		return dataService.restatus(dataId);
	}

	@GetMapping("/info")
	public BaseResponse<String> status() {
		
		return new BaseResponse<String>().success("在线");
	}

	@PostMapping("/tableStructure")
	public BaseResponse<Map<String, Object>> tableStructure(
			@RequestParam("file") MultipartFile file,
			@RequestParam("datasourceId") Long datasourceId,
			@RequestParam("fileDir") String fileDir) {
		List<TableStructure> list = new ArrayList<TableStructure>();
		Map<String, Object> resp = new HashMap<String, Object>();
		if(null == file) {
			return new BaseResponse<Map<String, Object>>().error("请上传文件");
		}
		try {
			list = CsvUtil.readCSVStructure(file);
			if(null == list || list.size() == 0) {
				return new BaseResponse<Map<String, Object>>().error("表格结构读取失败");
			}
			KusciaDatasourceOssEvent ossInfo = dataSourceService.getDatasourceOss(datasourceId);
			if(null == ossInfo) {
				return new BaseResponse<Map<String, Object>>().error("数据源不存在");
			}
			AwsOssConfig ossConfig = AwsOssConfig.builder().endpoint(ossInfo.getOssEndpoint()).accessKeyId(ossInfo.getOssAccessKeyId())
					.secretAccessKey(ossInfo.getOssAccessKeySecret()).build();
			BaseResponse<String> checkRes = ossService.checkBucketExists(ossConfig, ossInfo.getOssBucket());
			if(checkRes.getStatus() != 0) {
				return new BaseResponse<Map<String, Object>>().error(checkRes.getMsg());
			}
			BaseResponse<FileUploadRes> ossRes = ossService.upload(file, ossConfig, ossInfo.getOssBucket(), ossInfo.getOssPrefix(), fileDir, file.getOriginalFilename());
			if(ossRes.getStatus() == 0) {
				resp.put("structures", list);
				resp.put("uploadRes", ossRes);
				return new BaseResponse<Map<String, Object>>().success("读取成功").setData(resp);
			}
			return new BaseResponse<Map<String, Object>>().error("文件操作失败:" +  ossRes.getMsg());
		} catch (Exception e) {
			logger.error("读取文件异常", e);
			return new BaseResponse<Map<String, Object>>().error("文件操作失败");
		}
	}

	@GetMapping("/showResultData")
	public BaseResponse<String> showResultData(Long projectId, Long flowId, String domainDataId) {
		
		return new BaseResponse<String>().success("查询文件");
	}
	
}
